/*
 * Copyright 2009 Gary Brown
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 *
 * Change History:
 * 7 Mar 2009 : Initial version created by gary
 */
package org.betonthemove.ui.android;

import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.*;

public class PlaceBetView extends Activity implements SeekBar.OnSeekBarChangeListener,
							OnClickListener {

	public static final String MARKET_DATA_ATTR = "org.betonthemove.MarketData";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.placebet);
        
        TextView betSummaryField=(TextView)findViewById(R.id.betSummary);
        
        m_oddsDisplay = (TextView)findViewById(R.id.oddsDisplay);
        m_stakeDisplay = (TextView)findViewById(R.id.stakeDisplay);
        
        m_marketEntrantData =(MarketData)getIntent().getSerializableExtra(MARKET_DATA_ATTR);
        
        betSummaryField.setText(m_marketEntrantData.getSelectionName()+
        		" ["+(m_marketEntrantData.isBack()?"Back":"Lay")+"]");
        
        for (m_oddsStart=0; (m_marketEntrantData.getPrice()-
        		m_odds.get(m_oddsStart).getValue()) >= 0.005; m_oddsStart++) {
        }
        
        if (Log.isLoggable("PlaceBetView", Log.INFO)) {
	        double price=m_marketEntrantData.getPrice();
	        double odds=m_odds.get(m_oddsStart).getValue();
	        double diff=(m_marketEntrantData.getPrice()-
	        		m_odds.get(m_oddsStart).getValue());
        	Log.i("PlaceBetView", "Price="+price+" Odds="+odds+" Diff="+diff+
        					" Position="+m_oddsStart);
        }
        
    	m_oddsStart -= 50;
        
        if (m_oddsStart < 0) {
        	m_oddsStart = 0;
        }
                
        m_oddsBar = (SeekBar)findViewById(R.id.odds);
        m_oddsBar.setOnSeekBarChangeListener(this);
        
        int position=getOddsProgress(m_marketEntrantData.getPrice());
        m_oddsBar.setProgress(position);
        
        m_stakeBar = (SeekBar)findViewById(R.id.stake);
        m_stakeBar.setMax(m_stakes.size()-1);
        m_stakeBar.setOnSeekBarChangeListener(this);
        
        //position=getStakeProgress(m_marketEntrantData.getStake());
        position = getStakeProgress(2);
        
        m_stakeBar.setProgress(position);
        
        displayOdds(m_oddsBar);
		displayStake(m_stakeBar);       
		
		m_betButton = (Button)findViewById(R.id.placebet);
		m_betButton.setOnClickListener(this);
		
		m_cancelButton = (Button)findViewById(R.id.cancelbet);
		m_cancelButton.setOnClickListener(this);
    }
    
    protected int getOddsProgress(double odds) {
    	int pos=0;
 
    	for (pos=0; (odds - m_odds.get(pos).getValue()) >= 0.005; pos++) {
        }

    	if (Log.isLoggable("PlaceBetView", Log.INFO)) {
    		Log.i("PlaceBetView", "Odds="+odds+
    			" Position="+pos+" - oddsStart("+m_oddsStart+")");
    	}
    	
    	return(pos-m_oddsStart);
    }
    
    protected double getProgressOdds(int progress) {
    	double ret=m_odds.get(progress+m_oddsStart).getValue();
    	
    	ret *= 100;
    	ret = ((int)Math.round(ret))/100.0;
    	
    	return(ret);
    }
    
    protected int getStakeProgress(double stake) {
    	int pos=0;
    	 
    	for (pos=0; m_stakes.get(pos).intValue() < stake; pos++) {
        }
    	
    	return(pos);
    }
    
    protected double getProgressStake(int progress) {
    	return(m_stakes.get(progress));
    }

    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromTouch) {
    	if (seekBar == m_oddsBar) {
    		displayOdds(seekBar);
    	} else if (seekBar == m_stakeBar) {
    		displayStake(seekBar);
    	}
    }
    
    protected void displayOdds(SeekBar bar) {
		m_oddsDisplay.setText("Odds: "+getProgressOdds(bar.getProgress()));
    }

    protected void displayStake(SeekBar bar) {
    	m_stakeDisplay.setText("Stake: "+getProgressStake(bar.getProgress()));    	
    }

    public void onStartTrackingTouch(SeekBar seekBar) {
        //mTrackingText.setText(getString(R.string.seekbar_tracking_on));
    }

    public void onStopTrackingTouch(SeekBar seekBar) {
        //mTrackingText.setText(getString(R.string.seekbar_tracking_off));
    }
    
    public void onClick(View button) {
    	if (button == m_cancelButton) {
    		finish();
    	} else if (button == m_betButton) {
    		double odds=getProgressOdds(m_oddsBar.getProgress());
    		double stake=getProgressStake(m_stakeBar.getProgress());
    		
    		MarketData md=new MarketData(m_marketEntrantData.getMarketId(),
    				m_marketEntrantData.getSelectionId(),
    				m_marketEntrantData.getSelectionName(),
    				m_marketEntrantData.isBack(),
    				odds, stake);
    		
            Intent intent = new Intent();
            intent.setClass(PlaceBetView.this, BetResultView.class);
            
            intent.putExtra(PlaceBetView.MARKET_DATA_ATTR, md);
            
            startActivity(intent);
            
    		finish();
    	}
    }
	
	public void onConfigurationChanged(Configuration newConfig) {
		super.onConfigurationChanged(newConfig);		
	}
	
    private MarketData m_marketEntrantData=null;
    private SeekBar m_oddsBar=null;
    private SeekBar m_stakeBar=null;
    private TextView m_oddsDisplay=null;
    private TextView m_stakeDisplay=null;
    private Button m_cancelButton=null;
    private Button m_betButton=null;
    private int m_oddsStart=0;
    
    public static class Odd {
    	public Odd(double odd) {
    		m_odd = odd;
    	}
    	
    	public double getValue() {
    		return(m_odd);
    	}
    	
    	public boolean equals(Object obj) {
    		boolean ret=false;
    	
    		if (obj instanceof Odd) {
    			Odd other=(Odd)obj;
    			
    			if (other.m_odd == m_odd) {
    				ret = true;
    			}
    		}
    
    		return(ret);
    	}
    	
    	public int hashCode() {
    		return((int)(m_odd*100));
    	}
    	
    	private double m_odd=0.0;
    }
    
    static java.util.List<Odd> m_odds=new java.util.Vector<Odd>();
    static java.util.List<Integer> m_stakes=new java.util.Vector<Integer>();
    
    static {
    	for (double i=1.01; i < 2.0; i += 0.01) {
    		m_odds.add(new Odd(i));
    	}
    	for (double i=2.0; i < 3.0; i += 0.02) {
    		m_odds.add(new Odd(i));
    	}
    	for (double i=3.0; i < 4.0; i += 0.05) {
    		m_odds.add(new Odd(i));
    	}
    	for (double i=4.0; i < 6.0; i += 0.1) {
    		m_odds.add(new Odd(i));
    	}
    	for (double i=6.0; i < 10.0; i += 0.2) {
    		m_odds.add(new Odd(i));
    	}
    	for (double i=10.0; i < 20.0; i += 0.5) {
    		m_odds.add(new Odd(i));
    	}
    	for (double i=20.0; i < 30.0; i += 1.0) {
    		m_odds.add(new Odd(i));
    	}
    	for (double i=30.0; i < 50.0; i += 2.0) {
    		m_odds.add(new Odd(i));
    	}
    	for (double i=50.0; i < 100.0; i += 5.0) {
    		m_odds.add(new Odd(i));
    	}
    	for (double i=100.0; i < 1000.0; i += 10.0) {
    		m_odds.add(new Odd(i));
    	}
    	
    	for (int i=2; i < 50; i++) {
    		m_stakes.add(i);
    	}
    	for (int i=50; i < 70; i += 2) {
    		m_stakes.add(i);
    	}
    	for (int i=70; i < 100; i += 5) {
    		m_stakes.add(i);
    	}
    	for (int i=100; i < 400; i += 10) {
    		m_stakes.add(i);
    	}
    	for (int i=400; i < 600; i += 20) {
    		m_stakes.add(i);
    	}
    	for (int i=600; i < 1000; i += 50) {
    		m_stakes.add(i);
    	}
    }
}
